//
//  AmuseMenuViewCell.swift
//  
//
//  Created by 刘耀 on 2017/4/14.
//
//

import UIKit

private let kAmuseMenuViewCellID = "kAmuseMenuViewCellID"

class AmuseMenuViewCell: UICollectionViewCell {
    
    // MARK: - 数组模型
    var gropus: [AnchorGroup]? {
        didSet {
            
            collectionView.reloadData()
        }
    }

    @IBOutlet weak var collectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        autoresizingMask = .init(rawValue: 0)
        
        // 注册Cell
        collectionView.register(UINib(nibName: "CollectionGameCell", bundle: nil), forCellWithReuseIdentifier: kAmuseMenuViewCellID)
        // 给collectionView添加内边距
//        collectionView.contentInset = UIEdgeInsets(top: 0, left: kEdgeInsetMargin, bottom: 0, right: kEdgeInsetMargin)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        let itemW = collectionView.width / 4
        let itemH = collectionView.height / 2
        layout.itemSize = CGSize(width: itemW, height: itemH)  // 这个尺寸如果放在 awakeFromNib() 不对，这里拿到的是xib的大小。放在这里拿到的是外界给定的尺寸大小
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
    }

}


// MARK: - UICollectionViewDataSource
extension AmuseMenuViewCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return gropus?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kAmuseMenuViewCellID, for: indexPath) as! CollectionGameCell
        
        cell.baseGame = gropus![indexPath.item]
        cell.clipsToBounds = true
        return cell
    }
    
}
